package com.waterworld.watch.chat.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.waterworld.watch.R;
import com.waterworld.watch.chat.adapter.EditGroupAdapter;
import com.waterworld.watch.chat.interfaces.IChatManager;
import com.waterworld.watch.chat.manager.ChatManager;
import com.waterworld.watch.common.activity.BaseActivity;
import com.waterworld.watch.common.bean.ChatGroupBean;
import com.waterworld.watch.common.bean.GroupAllowUserBean;
import com.waterworld.watch.common.bean.GroupMemberBean;
import com.waterworld.watch.common.bean.UserInfoBean;
import com.waterworld.watch.common.net.BaseObserverListener;
import com.waterworld.watch.common.net.BaseResultBean;
import com.waterworld.watch.common.util.AppManager;
import com.waterworld.watch.common.util.ScreenAdapterUtil;
import com.waterworld.watch.common.util.ToastUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EditGroupActivity extends BaseActivity {

    @BindView(R.id.header_parent)
    LinearLayout header_parent;

    @BindView(R.id.header_back)
    ImageView header_back;

    @BindView(R.id.header_title)
    TextView header_title;

    @BindView(R.id.header_confirm)
    TextView header_confirm;

    @BindView(R.id.rv_group)
    RecyclerView rv_group;

    @BindView(R.id.et_group_name)
    EditText et_group_name;

    @BindView(R.id.btn_dismiss_group)
    Button btn_dismiss_group;

    private IChatManager iChatManager = ChatManager.getInstance();
    private List<GroupMemberBean> mList = new ArrayList<>();
    private EditGroupAdapter mAdapter;

    private ChatGroupBean bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_group);
        ButterKnife.bind(this);
        bean = (ChatGroupBean) getIntent().getSerializableExtra("chatGroup");
        initView();
    }

    private void initView() {
        setViewSize(header_parent, LinearLayout.LayoutParams.MATCH_PARENT, ScreenAdapterUtil.getHeightPx(this)/10);
        header_back.setVisibility(View.VISIBLE);
        header_title.setText(R.string.edit_group);
        header_title.setVisibility(View.VISIBLE);
        header_confirm.setVisibility(View.VISIBLE);
        et_group_name.setText(bean.getName());
        if (bean.getGroupType() == 2) {
            if (!UserInfoBean.getInstance().getUser().getUserId().equals(bean.getOwner())) {
                btn_dismiss_group.setText("退群");
            } else {
                btn_dismiss_group.setText("解散该群组");
            }
        } else {
            btn_dismiss_group.setVisibility(View.GONE);
        }
    }

    private void initData() {
        mList.clear();
        if (iChatManager != null) {
            iChatManager.listGroupMember(bean.getGroupId(), new BaseObserverListener<BaseResultBean<GroupMemberBean[]>>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    ToastUtils.showShort(EditGroupActivity.this, getString(R.string.data_error));
                }

                @Override
                public void onNext(BaseResultBean<GroupMemberBean[]> baseResultBean) {
                    mList.addAll(Arrays.asList(baseResultBean.getData()));
                    if (bean.getGroupType() == 2 && Objects.equals(UserInfoBean.getInstance().getUser().getUserId(), bean.getOwner())) {
                        GroupMemberBean add_bean = new GroupMemberBean();
                        add_bean.setHead("ic_group_add");
                        add_bean.setMemberName(getString(R.string.add));
                        mList.add(add_bean);

                        GroupMemberBean delete_bean = new GroupMemberBean();
                        delete_bean.setHead("ic_group_delete");
                        delete_bean.setMemberName(getString(R.string.delete));
                        mList.add(delete_bean);
                    }

                    mAdapter = new EditGroupAdapter(EditGroupActivity.this,mList);
                    rv_group.setLayoutManager(new GridLayoutManager(EditGroupActivity.this,4));
                    rv_group.setAdapter(mAdapter);

                    if (bean.getGroupType() == 2 && Objects.equals(UserInfoBean.getInstance().getUser().getUserId(), bean.getOwner())) {
                        mAdapter.setOnItemClickListener(new EditGroupAdapter.Listener() {
                            @Override
                            public void onItemClick(View view, int position) {
                                if (position == mList.size() - 2) {
                                    startActivity(new Intent(EditGroupActivity.this, EditMemberActivity.class).putExtra("title", "add").putExtra("ChatGroupBean", bean));
                                } else if (position == mList.size() - 1) {
                                    startActivity(new Intent(EditGroupActivity.this, EditMemberActivity.class).putExtra("title", "delete").putExtra("ChatGroupBean", bean));
                                }
                            }
                        });
                    } else {
                        mAdapter.setOnItemClickListener(new EditGroupAdapter.Listener() {
                            @Override
                            public void onItemClick(View view, int position) {
                                //nothing to do
                            }
                        });
                    }
                }
            });
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    @OnClick(R.id.header_back)
    void onBack() {
        finish();
    }

    @OnClick(R.id.header_confirm)
    void updateGroupName() {
        if (!TextUtils.isEmpty(et_group_name.getText().toString().trim())) {
            if (iChatManager != null) {
                iChatManager.updateGroupName(bean.getGroupId(), et_group_name.getText().toString().trim(), new BaseObserverListener<BaseResultBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(BaseResultBean baseResultBean) {
                        if (baseResultBean.getCode() == 0) {
                            finish();
                        }
                    }
                });
            }
        } else {
            ToastUtils.showShort(this,"群组名称不能为空!");
        }
    }

    @OnClick(R.id.btn_dismiss_group)
    void onDismiss() {
        if (UserInfoBean.getInstance().getUser().getUserId().equals(bean.getOwner())) {
            //管理员删除群
            if (iChatManager != null) {
                iChatManager.deleteGroup(bean.getGroupId(), new BaseObserverListener<BaseResultBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(BaseResultBean baseResultBean) {
                        if (baseResultBean.getCode() == 0) {
                            finish();
                            AppManager.finishActivity(ChatActivity.class);
                        }
                    }
                });
            }
        } else {
            //普通人员退群
            if (iChatManager != null) {
                iChatManager.quitGroup(bean.getGroupId(), new BaseObserverListener<BaseResultBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(BaseResultBean baseResultBean) {
                        if (baseResultBean.getCode() == 0) {
                            finish();
                        }
                    }
                });
            }
        }
    }

}
